package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.AdminGoodsMapper;
import cn.scxy.bookcity.pojo.dto.AdminGoodsDTO;
import cn.scxy.bookcity.pojo.entity.Imgs;
import cn.scxy.bookcity.pojo.vo.*;
import cn.scxy.bookcity.service.AdminGoods;
import cn.scxy.bookcity.web.ServiceCode;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 张正荣
 */
@Service
@Slf4j
public class AdminGoodsImpl implements AdminGoods {
    @Autowired
    private AdminGoodsMapper mapper;
    @Override
    public List<AdminGoodsCateList> adminSelectGoodsAddNew() {
        List<AdminGoodsCateList> a=new ArrayList<>();
        List<AdminGoodsCateList> adminGoodsCateLists = mapper.adminSelectGoodsAddNew();//全部查询
//        for (AdminGoodsCateList adminGoodsCateList : adminGoodsCateLists) {
//            if(adminGoodsCateList.getIsParent().equals(1)){//是父级
//                if(adminGoodsCateList.getDepth()==1){//一级类别
//                    adminGoodsCateList.setDisabled(true);
//                }
//                Long id = adminGoodsCateList.getId();
//                List<AdminGoodsCateList> adminGoodsCateLists1 = mapper.selectChildrenById(id);//子集id
//                for (AdminGoodsCateList goodsCateList : adminGoodsCateLists1) {
//                    a.add(goodsCateList);
//                }
//                adminGoodsCateList.setChildren(a);
//            }
//        }
        return adminGoodsCateLists;
    }

    @Override
    public List<PressListVO> selectPressList() {
        List<PressListVO> pressListVOS = mapper.selectPressList();
        return pressListVOS;
    }

    @Override
    public List<PackListVO> selectPackList() {
        List<PackListVO> packListVOS = mapper.selectPackList();
        return packListVOS;
    }

    @Override
    public String packByid(Long name) {
        String aLong = mapper.selectpackByid(name);
        return aLong;
    }
    @Override
    public String  pressByid(Long name) {
        String aLong = mapper.selectpressByid(name);
        return aLong;
    }

    @Value("${dirPath}")
    private String dirPath;
    @Override
    public void insert(AdminGoodsDTO adminGoodsDTO) {
        adminGoodsDTO.setCreateTime(new Date());
         mapper.insertGoods(adminGoodsDTO);//插入商品
        Integer id = adminGoodsDTO.getId();
        log.debug("adminGoodsDTO={}",id);
        String url = adminGoodsDTO.getUrl();
        log.debug("url={}",url);
        StringBuilder s= new StringBuilder(url);
//        StringBuilder delete = s.delete(0, 7);
//        log.debug("删除{}",delete);//0=%2F4dde40eb-4a8d-4f41-9f6f-e413cff3fc78.jpg&1=%2F5e8ed083-9051-413a-bad6-5b8bfcb0ee2e.jpg
        String s1 = s.toString();
        String[] split = s1.split("&");
        log.debug("截取字符串尾{}",split);
        for (int i = 0; i < split.length; i++) {
            String[] split1 = split[i].split("%2F");
            log.debug("结果{}",split1[1]);
            Imgs imgs=new Imgs();
            imgs.setUrl("http://localhost:8080"+"/"+split1[1]);
            imgs.setGId(id);
            mapper.insertImg(imgs);
        }


    }

    @Override
    public List<GoodsListItemVO> selectgoodslist() {
       List<GoodsListItemVO> list= mapper.selectgoodslist();
        return list;
    }

    @Override
    public void deleteGoodsById(Integer id) {
        mapper.deleteGoodsById(id);
        mapper.deleteImgsById(id);
    }
    @Override
    public void setEnable(Integer id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Integer id) {
        updateEnableById(id, 0);
    }


    private void updateEnableById(Integer id, Integer enable) {
        String[] tips = {"下架", "上架"};
        log.debug("开始处理【{}类别】的业务，参数：{}", tips[enable], id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        GoodsListItemVO goodsById = mapper.getGoodsById(id);
        // 判断查询结果是否为null
        if (goodsById == null) {
            String message = tips[enable] + "商品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (goodsById.getState().equals(enable)) {
            String message = tips[enable] + "商品失败，当前类别已经处于" + tips[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        GoodsListItemVO goods = new GoodsListItemVO();
        goods.setId(id);
        goods.setState(enable);
        int rows = mapper.update(goods);
        if (rows != 1) {
            String message = tips[enable] + "类别失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public GoodsListItemVO selectGoodsById(Integer id) {
        GoodsListItemVO goodsListItemVO=mapper.selectGoodsById(id);
        return goodsListItemVO;
    }

    @Override
    public List<ImgsListItemVO> selectImgById(Integer id) {
        List<ImgsListItemVO> list=mapper.selectImgById(id);
        return list;
    }

    @Override
    public void updateGoods(GoodsListItemVO goodsListItemVO) {
        log.debug("修改商品信息{}",goodsListItemVO);
        String s= goodsListItemVO.getNum().toString();
        goodsListItemVO.setNum(s);
        mapper.update(goodsListItemVO);
    }

    @Override
    public void deleteUrlById(Integer id) {
        mapper.deleteUrlById(id);
    }

    @Override
    public void insertUrl(ImgsListItemVO imgsListItemVO) { ;
        imgsListItemVO.setUrl("http://localhost:8080"+imgsListItemVO.getUrl());
        mapper.insertImgById(imgsListItemVO);

    }

    @Override
    public List<GoodsListItemVO> selectLikeGoods(GoodsListItemVO goodsListItemVO) {
      List<GoodsListItemVO> list= mapper.selectLikeGoods(goodsListItemVO);
        return list;
    }

    @Override
    public int selectGoodsCount() {
        int i=mapper.selectGoodsCount();
        return i;
    }

    @Override
    public List<PackListVO> packlist() {
        List<PackListVO> list= mapper.packlist();
        return list;
    }

    @Override
   public List<PressListVO> presslist() {
        List<PressListVO> list= mapper.presslist();
        return list;
    }

    @Override
    public void deletePack(Integer id) {
       int list=mapper.selectGoodsByPackId(id);
       log.debug("统计值{}，",list);
       if(list==0){
           mapper.delectPack(id);
       }else{
           throw new ServiceException(ServiceCode.ERR_DELETE,"删除失败有商品正在使用该包装类型!!");
       }
    }
    @Override
    public void deletePress(Integer id) {
       int list=mapper.selectGoodsByPressId(id);
       log.debug("统计值{}，",list);
       if(list==0){
           mapper.deletePress(id);
       }else{
           throw new ServiceException(ServiceCode.ERR_DELETE,"删除失败有商品正在使用该出版社类型!!");
       }
    }

    @Override
    public void updatePackName(PackListVO packListVO) {
        mapper.updatePackName(packListVO);
    }
    @Override
    public void updatePressName(PressListVO pressListVO) {
        mapper.updatePressName(pressListVO);
    }


    @Override
    public void insetPress(PressListVO pressListVO) {
        mapper.insetPress(pressListVO);
    }
    @Override
    public void insetPack(PackListVO packListVO) {
        mapper.insetPack(packListVO);
    }
}
